package cn.bblocks.common.i18n.demo.config;

import cn.bblocks.common.i18n.I;
import cn.bblocks.common.i18n.I18nHolder;
import cn.bblocks.common.i18n.demo.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;

/**
 * 统一异常处理及国际化
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handle(HttpServletRequest request, Exception e){
        log.info("异常：", e.getMessage());
        /**
         * 业务异常，提取业务异常码转具体错误，其他异常直接显示系统故障
         */
        Integer code = null;
        if(code == null){
            return R.failed();
        }else{
            return R.failed(code,null);
        }
    }


    /**
     * 表单、json都触发
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public R handleBindException(HttpServletRequest request, BindException e){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("请求参数格式错误，");
        stringBuilder.append(e.getFieldError().getDefaultMessage());
        stringBuilder.append("！");
        String message = stringBuilder.toString();
        log.warn("BindException参数交易失败:{}",message);

        log.info("lang:{}",I18nHolder.getI18nLocaleHolder().getLocale().toLanguageTag());
        String i18message = I.n(e.getFieldError().getDefaultMessage());
        return R.failed(i18message);
    }


    /**
     * 仅对json提交触发
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public R handleMethodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("请求参数格式错误，");
        stringBuilder.append(e.getFieldError().getDefaultMessage());
        stringBuilder.append("！");
        String message = stringBuilder.toString();
        log.warn("MethodArgumentNotValidException参数交易失败:{}",message);

        log.info("lang:{}",I18nHolder.getI18nLocaleHolder().getLocale().toLanguageTag());
        String i18message = I.n(e.getFieldError().getDefaultMessage());
        return R.failed(i18message);
    }


    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public R handleConstraintViolationException(HttpServletRequest request, ConstraintViolationException e){
        log.warn("ConstraintViolationException参数交易失败:{}",e.getMessage());

        String i18message = e.getMessage();
        return R.failed(i18message);
    }
}
